﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSP.Common.Providers;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Collections.ObjectModel;

namespace CSP.Common.Factories
{
    /// <summary>
    /// 图片工厂,暂提供上传图片和图片处理功能
    /// </summary>
    public class ImageProviderFactory
    {
        private string _savePath = "";

        private int _lHeigth = 600;  //设置生成上传服务器的图片

        private int _lWidth = 0;  //设置生成上传服务器的图片

        private int _sWidth = 300; //指定(附加图)缩略图的宽度 默认为300

        public string SavePath
        {
            get
            {
                return _savePath;
            }
            set
            {
                _savePath = value;
            }
        }

        /// <summary>
        /// 指定原始图片图片缩放的宽度，如果为0 代表不缩略原始图片
        /// </summary>
        public int LWidth
        {
            get { return _lWidth = _imageProvider.LWidth; }
            set
            {
                _imageProvider.LWidth = _lWidth = value;

            }
        }

        /// <summary>
        /// 是否生成缩略图 默认true
        /// </summary>
        public bool IsCreateImg
        {
            get { return _imageProvider.IsCreateImg; }
            set { _imageProvider.IsCreateImg = value; }
        }

        /// <summary>
        /// 是否加水印 默认false
        /// </summary>
        public bool IsDraw
        {
            get { return _imageProvider.IsDraw; }
            set { _imageProvider.IsDraw = value; }
        }

        /// <summary>
        /// 指定(附加图)缩略图的宽度 默认为300
        /// </summary>
        public int SWidth
        {
            get { return _sWidth = _imageProvider.sWidth; }
            set
            {
                _imageProvider.sWidth = _sWidth = value;

            }
        }

        private ImageProvider _imageProvider;
        private ImageProviderFactory()
        {
            _imageProvider = new ImageProvider();   //图片提供者
        }

        /// <summary>
        /// 获取工厂实例
        /// </summary>
        /// <returns></returns>
        public static ImageProviderFactory GetInstance()
        {
            return new ImageProviderFactory();
        }

        /// <summary>
        /// 无缩略图的多图片上传,加水印
        /// </summary>
        /// <param name="str"></param>
        public ICollection<ImageProvider> UploadForNoThumbnails(HttpFileCollection fileCollection)
        {
            ICollection<ImageProvider> imageProviders = new Collection<ImageProvider>();
            string[] fileKeys = fileCollection.AllKeys;
            for (int i = 0; i < fileKeys.Length; i++)
            {
                imageProviders.Add(UploadForNoThumbnail(fileCollection[i]));
            }

            return imageProviders;
        }


        /// <summary>
        /// 无缩略图的上传,加水印
        /// </summary>
        /// <param name="str"></param>
        public ImageProvider UploadForNoThumbnail(HttpPostedFile postFile)
        {
            _imageProvider.IsCreateImg = false;
            _imageProvider._File_Upload = postFile;

            _imageProvider.Save_AS();
            return _imageProvider;
        }

        /// <summary>
        /// 有缩略图的对图片上传,加水印
        /// </summary>
        /// <param name="fileCollection"></param>
        /// <returns></returns>
        public ICollection<ImageProvider> UploadForThumbnails(HttpFileCollection fileCollection)
        {
            ICollection<ImageProvider> imageProviders = new Collection<ImageProvider>();
            string[] fileKeys = fileCollection.AllKeys;
            for (int i = 0; i < fileKeys.Length; i++)
            {
                imageProviders.Add(UploadForThumbnail(fileCollection[i]));
            }

            return imageProviders;
        }

        /// <summary>
        /// 有缩略图的上传,加水印
        /// </summary>
        /// <param name="postFile"></param>
        /// <returns></returns>
        public ImageProvider UploadForThumbnail(HttpPostedFile postFile)
        {
            this.InitImageConfig();             //初始化配置文件
            //_imageProvider.IsCreateImg = true;
            _imageProvider._File_Upload = postFile;

            _imageProvider.Save_AS();
            return _imageProvider;
        }

        /// <summary>
        /// 复制并生成新文件 ~/目录
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public string CopyAndCreateNewImg(string fileName)
        {
            string domainPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
            string currentFileName = domainPath + @fileName.Replace("~/", "");
            return _imageProvider.CopyAndCreateNewImg(currentFileName).Replace(domainPath, "~/");
        }

        /// <summary>
        /// 图片配置
        /// </summary>
        private void InitImageConfig()
        {
            if (_savePath == "")
            {
                _savePath = ConfigurationManager.AppSettings["Img.SavePath"].ToString();
            }
            string fileType = ConfigurationManager.AppSettings["Img.FileType"].ToString();


            string savePath = _savePath.Replace("~/", "");


            if (!string.IsNullOrWhiteSpace(fileType))
            {
                _imageProvider.FileType = fileType;
            }
            if (!string.IsNullOrWhiteSpace(savePath))
            {
                _imageProvider.SavePath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @savePath + DateTime.Now.ToString("yyyyMMdd") + "/";
            }
        }
    }
}
